package com.truckdispatching.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.truckdispatching.R;
import com.truckdispatching.UserManager;
import com.truckdispatching.http.controller.GetUserInfoHttpController;
import com.truckdispatching.http.controller.UpdateUserInfoHttpController;
import com.truckdispatching.http.util.OnResponseHandler;

/**
 * 修改用户信息界面
 * 
 * @author Lee
 * @date 2013-4-30
 */
public class ModifyUserInfoActivity extends BaseActivity implements OnClickListener {

	private Button mBtnBack;
	private TextView mBtnUploadPic;
	private TextView mChoseCarLength;
	private TextView mChoseCarType;
	private TextView mChoseTarget1;
	private TextView mChoseTarget2;
	private TextView mChoseTarget3;
	private Button mBtnOk;

	private TextView mRealName;
	private TextView mPhoneNumber;
	private EditText mAnotherMobile;
	private EditText mIDCard;
	private TextView mIDCardText;
	private TextView mCarNumber;

	private UpdateUserInfoHttpController mUpdateUserInfoHttpController;

	private GetUserInfoHttpController mGetUserInfoHttpController;

	private AlertDialog mSaveDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modify_userinfo_activity);

		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setOnClickListener(this);

		mBtnUploadPic = (TextView) findViewById(R.id.btn_upload);
		mBtnUploadPic.setOnClickListener(this);

		mChoseCarLength = (TextView) findViewById(R.id.userinfo_carlength);
		mChoseCarLength.setOnClickListener(this);
		mChoseCarType = (TextView) findViewById(R.id.userinfo_carltype);
		mChoseCarType.setOnClickListener(this);
		mChoseTarget1 = (TextView) findViewById(R.id.userinfo_place1);
		mChoseTarget1.setOnClickListener(this);
		mChoseTarget2 = (TextView) findViewById(R.id.userinfo_place2);
		mChoseTarget2.setOnClickListener(this);
		mChoseTarget3 = (TextView) findViewById(R.id.userinfo_place3);
		mChoseTarget3.setOnClickListener(this);

		mBtnOk = (Button) findViewById(R.id.btn_ok);
		mBtnOk.setOnClickListener(this);

		mRealName = (TextView) findViewById(R.id.userinfo_name);
		mRealName.setOnClickListener(this);
		mPhoneNumber = (TextView) findViewById(R.id.userinfo_mobile);
		mPhoneNumber.setOnClickListener(this);

		mAnotherMobile = (EditText) findViewById(R.id.userinfo_mobile2);
		mIDCard = (EditText) findViewById(R.id.userinfo_idcard);
		mIDCardText = (TextView) findViewById(R.id.userinfo_idcard_textview);
		mIDCardText.setOnClickListener(this);
		mCarNumber = (TextView) findViewById(R.id.userinfo_carnumber);
		mCarNumber.setOnClickListener(this);

		mRealName.setText(UserManager.getName());
		mPhoneNumber.setText(UserManager.getMobile());
		mAnotherMobile.setText(UserManager.getMobile2());
		String idcard = UserManager.getIdCard();
		if (idcard == null || idcard.equals("")) {
			mIDCard.setVisibility(View.VISIBLE);
			mIDCardText.setVisibility(View.GONE);
		} else {
			mIDCard.setVisibility(View.GONE);
			mIDCardText.setVisibility(View.VISIBLE);
			mIDCardText.setText(idcard);
		}
		mCarNumber.setText(UserManager.getTruckNumber());
		mChoseCarType.setText(UserManager.getTruckType());
		mChoseCarLength.setText(UserManager.getTruckLength());
		mChoseTarget1.setText(UserManager.getTarget1());
		mChoseTarget2.setText(UserManager.getTarget2());
		mChoseTarget3.setText(UserManager.getTarget3());

		mGetUserInfoHttpController = new GetUserInfoHttpController();
		mGetUserInfoHttpController.setOnResponseListener(new OnResponseHandler() {

			@Override
			public void onResponseError(int errorCode, String errorMessage) {
				showToast(errorMessage);
				dismissLoadingDialog();
			}

			@Override
			public void onResponse(Object response) {
				dismissLoadingDialog();
				mRealName.setText(UserManager.getName());
				mPhoneNumber.setText(UserManager.getMobile());
				mAnotherMobile.setText(UserManager.getMobile2());
				String idcard = UserManager.getIdCard();
				if (idcard == null || idcard.equals("")) {
					mIDCard.setVisibility(View.VISIBLE);
					mIDCardText.setVisibility(View.GONE);
				} else {
					mIDCard.setVisibility(View.GONE);
					mIDCardText.setVisibility(View.VISIBLE);
					mIDCardText.setText(idcard);
				}
				mCarNumber.setText(UserManager.getTruckNumber());
				mChoseCarType.setText(UserManager.getTruckType());
				mChoseCarLength.setText(UserManager.getTruckLength());
				mChoseTarget1.setText(UserManager.getTarget1());
				mChoseTarget2.setText(UserManager.getTarget2());
				mChoseTarget3.setText(UserManager.getTarget3());
			}
		});
		mGetUserInfoHttpController.sendRequest();
		showLoadingDialog();
	}

	private void showSaveDialog() {
		if (mSaveDialog == null) {
			mSaveDialog = new AlertDialog.Builder(this).setTitle("提示").setMessage("是否保存您的修改？").setPositiveButton("保存", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (mUpdateUserInfoHttpController != null) {
						mUpdateUserInfoHttpController.cancelHttpRequest();
					}
					final String mobile2 = mAnotherMobile.getText().toString().trim();
					if (!checkPhoneNumber(mobile2)) {
						return;
					}
					mUpdateUserInfoHttpController = new UpdateUserInfoHttpController();
					final String name = mRealName.getText().toString().trim();
					final String mobile = mPhoneNumber.getText().toString().trim();
					final String idCard;
					if (mIDCard.getVisibility() == View.VISIBLE) {
						idCard = mIDCard.getText().toString().trim();
					} else {
						idCard = mIDCardText.getText().toString().trim();
					}
					final String carLength = mChoseCarLength.getText().toString().trim();
					final String carNumber = mCarNumber.getText().toString().trim();
					final String carType = mChoseCarType.getText().toString().trim();
					final String target1 = mChoseTarget1.getText().toString().trim();
					final String target2 = mChoseTarget2.getText().toString().trim();
					final String target3 = mChoseTarget3.getText().toString().trim();
					mUpdateUserInfoHttpController.setOnResponseListener(new OnResponseHandler() {

						@Override
						public void onResponseError(int errorCode, String errorMessage) {
							showToast("修改失败：" + errorMessage);
							dismissLoadingDialog();
						}

						@Override
						public void onResponse(Object response) {
							showToast("修改成功");
							UserManager.setName(name);
							UserManager.setMobile(mobile);
							UserManager.setMobile2(mobile2);
							UserManager.setIdCard(idCard);
							UserManager.setTruckLength(carLength);
							UserManager.setTruckType(carType);
							UserManager.setTarget1(target1);
							UserManager.setTarget2(target2);
							UserManager.setTarget3(target3);
							dismissLoadingDialog();
							finish();
						}
					});
					mUpdateUserInfoHttpController.sendRequest(name, mobile, mobile2, idCard, carNumber, carLength, carType, target1, target2, target3);
					showLoadingDialog();
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			}).create();
		}
		mSaveDialog.show();
	}

	private boolean checkPhoneNumber(String phonenumber) {
		if (phonenumber.length() > 0 && phonenumber.length() != 11) {
			showToast("手机号格式错误");
			return false;
		}
		if (phonenumber.length() > 0 && !phonenumber.startsWith("1")) {
			showToast("手机号格式错误");
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back: {
			boolean isChanged = checkUserInfoChangeOrNot();
			if (isChanged) {
				showSaveDialog();
			} else {
				finish();
			}
			break;
		}
		case R.id.btn_ok: {
			boolean isChanged = checkUserInfoChangeOrNot();
			if (isChanged) {
				showSaveDialog();
			} else {
				showToast("用户资料没有变更");
			}
			break;
		}
		case R.id.btn_upload: {
			Intent intent = new Intent(this, UploadUserPictureActivity.class);
			startActivity(intent);
			break;
		}
		case R.id.userinfo_carlength: {
			Intent intent = new Intent(this, ChoseCarLengthActivity.class);
			startActivityForResult(intent, 1);
			break;
		}
		case R.id.userinfo_carltype: {
			Intent intent = new Intent(this, ChoseCarTypeActivity.class);
			startActivityForResult(intent, 5);
			break;
		}
		case R.id.userinfo_place1: {
			Intent intent = new Intent(this, ChoseProvinceActivity.class);
			startActivityForResult(intent, 2);
			break;
		}
		case R.id.userinfo_place2: {
			Intent intent = new Intent(this, ChoseProvinceActivity.class);
			startActivityForResult(intent, 3);
			break;
		}
		case R.id.userinfo_place3: {
			Intent intent = new Intent(this, ChoseProvinceActivity.class);
			startActivityForResult(intent, 4);
			break;
		}
		case R.id.userinfo_name: {
			showAlertDialog("真实姓名无法修改， 如需修改请打客服电话");
			break;
		}
		case R.id.userinfo_mobile: {
			showAlertDialog("手机号无法修改， 如需修改请打客服电话");
			break;
		}
		case R.id.userinfo_idcard_textview: {
			showAlertDialog("身份证无法修改， 如需修改请打客服电话");
			break;
		}
		case R.id.userinfo_carnumber: {
			showAlertDialog("车牌号无法修改， 如需修改请打客服电话");
			break;
		}
		default:
			break;
		}
	}

	private AlertDialog mAlertDialog;

	private void showAlertDialog(String message) {
		if (mAlertDialog == null) {
			mAlertDialog = new AlertDialog.Builder(this).setTitle("提示").setMessage(message).setPositiveButton("确定", null).create();
		} else {
			mAlertDialog.setMessage(message);
		}
		mAlertDialog.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == 100) {
			switch (requestCode) {
			case 1:
				String carLength = data.getExtras().getString("result");
				mChoseCarLength.setText(carLength);
				break;
			case 2:
				String place1 = data.getExtras().getString("result");
				if (place1.contains("不限")) {
					place1 = place1.split(" ")[0];
				}
				mChoseTarget1.setText(place1);
				break;
			case 3:
				String place2 = data.getExtras().getString("result");
				if (place2.contains("不限")) {
					place2 = place2.split(" ")[0];
				}
				mChoseTarget2.setText(place2);
				break;
			case 4:
				String place3 = data.getExtras().getString("result");
				if (place3.contains("不限")) {
					place3 = place3.split(" ")[0];
				}
				mChoseTarget3.setText(place3);
				break;
			case 5:
				String type = data.getExtras().getString("result");
				mChoseCarType.setText(type);
				break;
			default:
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		boolean isChanged = checkUserInfoChangeOrNot();
		if (isChanged) {
			showSaveDialog();
		} else {
			finish();
		}
	}

	private boolean checkUserInfoChangeOrNot() {
		String mobile2 = mAnotherMobile.getText().toString().trim();
		if (!mobile2.equals(UserManager.getMobile2())) {
			return true;
		}
		String idCard = mIDCard.getText().toString().trim();
		if (!idCard.equals(UserManager.getIdCard())) {
			return true;
		}
		String truckType = mChoseCarType.getText().toString().trim();
		if (!truckType.equals(UserManager.getTruckType())) {
			return true;
		}
		String truckLength = mChoseCarLength.getText().toString().trim();
		if (!truckLength.equals(UserManager.getTruckLength())) {
			return true;
		}
		String target1 = mChoseTarget1.getText().toString().trim();
		if (!target1.equals(UserManager.getTarget1())) {
			return true;
		}
		String target2 = mChoseTarget2.getText().toString().trim();
		if (!target2.equals(UserManager.getTarget2())) {
			return true;
		}
		String target3 = mChoseTarget3.getText().toString().trim();
		if (!target3.equals(UserManager.getTarget3())) {
			return true;
		}
		return false;
	}
}
